Flash Article

Controlling Media Components with Macromedia Flash MX 2004 Behaviors

Dan Carr

dancarrdesign.com

Displaying video content is easier than ever before in Macromedia Flash MX 2004 thanks to the new media components that display video saved in the FLV format.

The standard approach for applying settings to a component is to adjust its parameters in the Component Inspector panel while authoring in the FLA file. But what if you need dynamic control of the video display while the movie is running?

Conveniently enough, Flash components have functions and parameters you can manipulate through ActionScript. Even more helpful is the addition of behaviors in Flash MX 2004 that allow beginning and advanced users to add prebuilt ActionScript to buttons, screens, and other objects in a Flash movie.

This article provides six custom behaviors that I designed to access functional control over video files playing in a Media Playback component. These behaviors are simple and easy to use. With the included sample files you'll be able to seek to a cue point or a time in the video; designate a video source for the component or choose a video source from a list of choices in a List component; and set up simple toggle behaviors to control play, pause, or autosize properties.

Requirements

To complete this tutorial you will need to install the following software and files:

Macromedia Flash MX Professional 2004

Extension Manager 1.6


Video Update: Exporter and Updated Media Components

This update resolves issues identified with the Flash Video Exporter and Media Components in Flash MX Professional 2004. All users creating video projects in Flash on any language should install this update.

Media Behaviors extension

Tutorials and sample files:

To use this tutorial, you should also be familiar with the basics of creating and running movies using Macromedia Flash MX 2004. For more resources, check out the Flash Developer Center's Video Resources page.

Other assumptions and requirements:

  • Although this tutorial works with the supplied files, the Media components are available only in the Components panel in Flash MX Professional 2004.
  • The Media components display FLV formatted video files. You can export them using the Flash Video Exporter plug-in in any supported video editing application or export them directly from Flash MX 2004. (See the FLV topic in the Help pages.)
  • You must install the supplied MXP file using Extension Manager 1.6 before you can use the custom behaviors.

Behavior 1: Seek to Cue

The Seek to Cue behavior allows you to jump to a time saved in a cue point within the media component. The following instructions show you how to add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Select the component and open the Component Inspector panel.
  3. Enter the URL parameter with the path to the video and add a name and time to a cue point field.
  4. Add a button to the Stage and select it.
  5. Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Seek to Cue from the list. The behavior's dialog box opens (see Figure 1).

    figure 1

    Figure 1. The Seek to Cue dialog box shows the target MediaDisplay (myMediaControl1) selected and the target cue name (cue1) entered in the field below.

  6. In the dialog box, browse for the media playback instance in the target list in the top portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  7. In the Add Cue Point Name field, enter the name of the cue point you saved in the Component Inspector panel.
  8. Click OK.

That's it. Test the movie and click the button while the video is playing to see the video jump to the cuepoint time.

Here's the code that is added to the button:

// Start Seek to Cue Behavior

var m = myMediaControl1;
var q = m.getCuePoint("cue1");

m.play(Math.round(q.time));

// End Seek to Cue Behavior

	

Tips:

  • In the code, the cue time is rounded to the nearest second to produce the smoothest results for tracking. You can alter this to target shorter time signatures.
  • When trying to seek to a timecode or event in an FLV, the playhead often jumps to the wrong time. This is because you can only seek to a whole keyframe within the video. If you try to seek to any other time, the playhead jumps to the nearest whole keyframe. The keyframe value is set at encode-time.

Behavior 2: Seek to Time

The Seek to Time behavior allows you to jump to a specific time that you specify. Make sure you choose a time that exists within the length of the video to see the effect work. The following instructions show you how to add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Select the component and open the Component Inspector panel.
  3. Enter the URL parameter with the path to the video.
  4. Add a button to the Stage and select it.
  5. Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Seek to Time from the list. The behavior's dialog box opens (see Figure 2).

    figure 2

    Figure 2. The Seek to Time dialog box shows the target MediaDisplay (myMediaControl2) selected and the target time (1 sec.) entered in the field below.

  6. In the dialog box, browse for the media playback instance in the target list in the top portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  7. In the Time to Seek To field, enter the number of seconds you would like to seek to.
  8. Click OK.

That's it. Test the movie and click the button while the video is playing to see the video jump to the time you specified.

Here's the code that is added to the button:

// Start Seek to Time Behavior
	
var m = myMediaControl2 
m.play(Math.round(1));

// End Seek to Time Behavior

Tips:

  • In the code, the cue time is rounded to the nearest second to produce the smoothest results for tracking. You can alter this to target shorter time signatures.
  • When trying to seek to a timecode or event in an FLV, the playhead often jumps to the wrong time. This is because you can only seek to a whole keyframe within the video. If you try to seek to any other time, the playhead jumps to the nearest whole keyframe. The keyframe value is set at encode-time.

Behavior 3: Set URL from List Component

The Set URL from List Component behavior allows you to set up a list of video choices in a List component and then load the selected video from the list as the source of the media component. The list should specify valid paths to the FLV files in the Data property in each item contained in the list. The Labels property for each list item can contain names for each video that are user-friendly. The following instructions show you how to set up the List component and add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Add a List component to the Stage.
  3. Select the List component and view the component parameters in the Property inspector.
  4. Click the Labels parameter. A dialog box opens. Click the plus (+) icon and enter the label for the first video. Repeat this step to add as many labels as you need. Click OK when you are finished.
  5. Click the Data parameter. Another dialog box opens. Click the plus (+) icon to enter the actual path to the first video. (The path is usually a relative path to a video sitting next to the Flash file or in a nearby directory.) Repeat this step to add a path for each label you previously added, making sure the order of the video paths matches the order of the labels. Click OK when you are finished.
  6. Add a button to the Stage and select it.
  7. Open the Behaviors panel and click the plus (+) icon to access the list of Behavior categories. Select the Media Controls category and choose Set URL from List Component from the list. The behavior's dialog box opens (see Figure 3).

    figure 3

    Figure 3. The Set URL from List Component dialog box shows the target List (myListControl) and the target MediaDisplay (myMediaControl3) selected. The Autoplay video option is set to True.

  8. In the dialog box, browse for the list component instance in the target list in the top portion of the dialog box. This targets the list of video choices.
  9. Browse for the media component instance in the target list in the bottom portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  10. Set the Autoplay video option to True or False.
  11. Click OK.

Here's the code that is added to the button:

// Start Set URL From List Component Behavior

var m = myListBox; 
var n = myMediaControl3; 
var p = true;

n.setMedia(m.selectedItem.data);

if (p) 
  n.play(); 
else 
  n.stop();
	 
// End Set URL From List Component Behavior

Tips:

  • When using this behavior, it's a good idea to make sure that the List has a default item selected. If the button that sets the video source is clicked and the List's selectedItem property is undefined, no video will be loaded. The sample FLA contains a code example on the timeline of the screen for Behavior 3.
  • At the time I wrote this article, there is a known issue with the setMedia function when loading the same FLV file repetitively. If a button containing this behavior is clicked in repetition to call the same video file, the video may not appear on some clicks. Loading different FLV files each time works without error, however.

Behavior 4: Set Video Stream

The Set Video Stream behavior allows you to enter the path to the video to be displayed. The following instructions show you how to add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Add a button to the Stage and select it.
  3. Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Set Video Stream from the list. The behavior's dialog box opens (see Figure 4).

    figure 4

    Figure 4. The Set Video Stream dialog box shows the target MediaDisplay (myMediaControl4) selected. The path to the FLV video source is entered in the field below.

  4. Browse for the media playback instance in the target list in the bottom portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  5. Enter the path to the FLV video file.
  6. Set the Autoplay video option to True or False.
  7. Click OK.

Here's the code that is added to the button:

// Start Set Video Stream Behavior
 
var m = myMediaControl4; 
var p = true;
 
m.setMedia("myMovie.flv"); 

if (p) 
  n.play(); 
else 
  n.stop(); 

// End Set Video Stream Behavior

Tip:

  • At the time I wrote this article, there is a known issue with the setMedia function when loading the same FLV file repetitively. If a button containing this behavior is clicked in repetition to call the same video file, the video may not appear on some clicks. Loading different FLV files each time works without error, however.

Behavior 5: Toggle AutoSize

The Toggle AutoSize behavior allows you to add code to a button so that it toggles the component's autosize property between True and False. The following instructions show you how to add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Add a button to the Stage and select it.
  3. Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Toggle AutoSize from the list. The behavior's dialog opens (see Figure 5).

    figure 5

    Figure 5. The Toggle AutoSize dialog box shows the target MediaDisplay (myMediaControl5) selected.

  4. Browse for the media playback instance in the target list in the bottom portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  5. Click OK.

Here's the code that is added to the button:

// Start Toggle AutoSize Behavior 

var m = myMediaControl5 
m.autoSize = !(m.autoSize); 

// End Toggle AutoSize Behavior

Tip:

  • To achieve the best results with the Toggle AutoSize effect, the MediaDisplay should be larger than the video, as seen in the sample FLA file.

Behavior 6: Toggle Play/Pause

The Toggle Play/Pause behavior allows you to add code to a button so that it toggles between play and pause when a video is running in the component. The following instructions show you how to add this behavior to a custom button.

To use this behavior:

  1. Add a Media Playback component to the Stage.
  2. Add a button to the Stage and select it.
  3. Open the Behaviors panel and click the plus (+) icon to access the list of behavior categories. Select the Media Controls category and choose Toggle Play/Pause from the list. The behavior's dialog box opens (see Figure 6).

    figure 6

    Figure 6. The Toggle Play/Pause dialog box shows the target MediaDisplay (myMediaControl6) selected.

  4. Browse for the media display instance in the target list in the bottom portion of the dialog box. This targets the instance name of the component displaying the video. A best practice is to use the Relative option for the path, as shown above. If you have problems adding paths to component buttons, try using the Absolute option instead.
  5. Click OK.

Here's the code that is added to the button:

// Start Toggle Play/Pause Behavior

var m = myMediaControl6;
if(!m.playing)
{ 
  if(Math.round(m.playheadTime) == m.totalTime) 
  m.stop();

  m.play();
}
else{
  m.pause(); 
}

// End Toggle Play/Pause Behavior

Tip:

You can remove the two lines of code above the m.play() line to make the video only play and pause once. In this case, when it reaches the end, it is not reset.

Conclusion

The Behaviors panel is a great new feature in Macromedia Flash MX 2004 that provides an easy way to add ActionScript to your movies. The six supplied behaviors allow you to set up custom buttons to control Media Display components without writing any code.


About the author

Dan Carr is owner, lead developer, and trainer for Dan Carr Design in San Francisco. With years of history developing for Macromedia, Dan has created a range of features for Flash, including e-learning templates and UI components for Flash MX Professional 2004. Dan teaches Flash design and ActionScript at CIBER Training in San Francisco and develops e-learning and web applications for the public, as well as Macromedia product teams. Contact Dan at www.dancarrdesign.com and look for his upcoming website launch featuring his recent work and new support materials.